
package com.wyzz.hr.action;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.bo.IEmpQuitBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empquit;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class SearchEmpQuitAction extends BaseDownloadAction {

	public SearchEmpQuitAction() {
		page = new Pager();
		outputIoName = "OEmployeeQuit";
		searchOrExport = null;
	}

	public String execute() throws Exception {
		getDrillDownList();
		quitList = searchEmpQuit();
		if ("export".equals(searchOrExport))
			return downloadToExcel(quitList, outputIoName, outmatchModelId, "");
		else
			return "success";
	}

	private List searchEmpQuit() {
		DetachedCriteria dcEmpQuit = searchEmpWithQuit_DC();
		addCriteria(dcEmpQuit);
		IEmpQuitBo empQuit = (IEmpQuitBo) getBean("empQuitBo");
		return empQuit.findEmpQuit(dcEmpQuit, page, searchOrExport);
	}

	private DetachedCriteria searchEmpWithQuit_DC() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Employee.class);
		dc.createAlias(Employee.PROP_EMP_QUIT_ID, "quit", 1);
		dc.createAlias(Employee.PROP_EMP_DEPT_NO, "empDeptNo", 1);
		dc.createAlias((new StringBuilder()).append("quit.").append(
				Empquit.PROP_EQ_PERMISSION).toString(), "manager", 1);
		if ("export".equals(searchOrExport)) {
			dc.setFetchMode(Employee.PROP_EMP_PB_NO, FetchMode.JOIN);
			dc.setFetchMode(Employee.PROP_EMP_LOCATION_NO, FetchMode.JOIN);
			dc.setFetchMode(Employee.PROP_EMP_TYPE, FetchMode.JOIN);
			dc.setFetchMode(Employee.PROP_EMP_BENEFIT_TYPE, FetchMode.JOIN);
		}
		return dc;
	}

	public void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "", 1);
		addOrders(dc, page, new String[] {
				(new StringBuilder()).append(Employee.PROP_EMP_TERMINATE_DATE)
						.append("-down").toString(),
				(new StringBuilder()).append(Employee.PROP_EMP_DISTINCT_NO)
						.append("-up").toString() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_STATUS, "eq",
				new Integer[] { Integer.valueOf(0) });
		BaseCrit.addEmpDC(dc, "", employeeId);
		BaseCrit.addDeptDC(dc, Employee.PROP_EMP_DEPT_NO,
				Employee.PROP_EMP_PB_NO, null, new Department(departmentId));
		BaseCrit
				.addDC(dc, (new StringBuilder()).append("quit.").append(
						Empquit.PROP_EQ_TYPE).toString(), "eq",
						new String[] { eqType });
		BaseCrit.addDC(dc, (new StringBuilder()).append("quit.").append(
				Empquit.PROP_EQ_REASON).toString(), "eq",
				new String[] { eqReason });
		BaseCrit.addEmpDC(dc, "manager", eqPermission);
		BaseCrit.addDC(dc, (new StringBuilder()).append("quit.").append(
				Empquit.PROP_ER_COMMENTS).toString(), "like",
				new String[] { eqComments });
		BaseCrit.addDCDateRange(dc, Employee.PROP_EMP_TERMINATE_DATE, eqDate,
				eqDateTo);
	}

	private void getDrillDownList() {
		eqTypeMap = ComonBeans.getValuesToMap(Empquit.PROP_EQ_TYPE,
				new boolean[] { true });
		eqReasonMap = ComonBeans.getSecondLevelSelect(eqTypeMap,
				Empquit.PROP_EQ_REASON, new boolean[] { true });
		eqRTypeMap = new TreeMap();
		eqRReasonMap = new TreeMap();
		eqRTypeMap.put("1", eqTypeMap.get("1"));
		eqRReasonMap.put("1", eqReasonMap.get("1"));
		eqTypeMap.remove("1");
		eqReasonMap.remove("1");
	}

	private void setQRMap() {
		eqTypeMap = ComonBeans.getValuesToMap(Empquit.PROP_EQ_TYPE,
				new boolean[] { true });
		eqReasonMap = ComonBeans.getSecondLevelSelect(eqTypeMap,
				Empquit.PROP_EQ_REASON, new boolean[] { true });
		eqRTypeMap = new TreeMap();
		eqRReasonMap = new TreeMap();
		eqRTypeMap.put("1", eqTypeMap.get("1"));
		eqRReasonMap.put("1", eqReasonMap.get("1"));
		eqTypeMap.remove("1");
		eqReasonMap.remove("1");
	}

	public Empquit getQuit() {
		return quit;
	}

	public void setQuit(Empquit quit) {
		this.quit = quit;
	}

	public Date getToDate() {
		return toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public List getManagerList() {
		return managerList;
	}

	public void setManagerList(List managerList) {
		this.managerList = managerList;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getExportFlag() {
		return exportFlag;
	}

	public void setExportFlag(String exportFlag) {
		this.exportFlag = exportFlag;
	}

	public List getQuitList() {
		return quitList;
	}

	public void setQuitList(List quitList) {
		this.quitList = quitList;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public String getEqType() {
		return eqType;
	}

	public void setEqType(String eqType) {
		this.eqType = eqType;
	}

	public String getEqReason() {
		return eqReason;
	}

	public void setEqReason(String eqReason) {
		this.eqReason = eqReason;
	}

	public String getEqPermission() {
		return eqPermission;
	}

	public void setEqPermission(String eqPermission) {
		this.eqPermission = eqPermission;
	}

	public String getEqComments() {
		return eqComments;
	}

	public void setEqComments(String eqComments) {
		this.eqComments = eqComments;
	}

	public Date getEqDate() {
		return eqDate;
	}

	public void setEqDate(Date eqDate) {
		this.eqDate = eqDate;
	}

	public Date getEqDateTo() {
		return eqDateTo;
	}

	public void setEqDateTo(Date eqDateTo) {
		this.eqDateTo = eqDateTo;
	}

	public Employee getQuitemp() {
		return quitemp;
	}

	public void setQuitemp(Employee quitemp) {
		this.quitemp = quitemp;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public void setOutputIoName(String outputIoName) {
		this.outputIoName = outputIoName;
	}

	public Map getEqTypeMap() {
		return eqTypeMap;
	}

	public Map getEqReasonMap() {
		return eqReasonMap;
	}

	public Map getEqRTypeMap() {
		return eqRTypeMap;
	}

	public void setEqRTypeMap(Map eqRTypeMap) {
		this.eqRTypeMap = eqRTypeMap;
	}

	public Map getEqRReasonMap() {
		return eqRReasonMap;
	}

	public void setEqRReasonMap(Map eqRReasonMap) {
		this.eqRReasonMap = eqRReasonMap;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	private List quitList;
	private String employeeId;
	private String departmentId;
	private String departmentName;
	private String eqType;
	private String eqReason;
	private String eqPermission;
	private String eqComments;
	private Date eqDate;
	private Date eqDateTo;
	private Empquit quit;
	private Employee quitemp;
	private Date toDate;
	private List managerList;
	private Pager page;
	private Map eqTypeMap;
	private Map eqReasonMap;
	private Map eqRTypeMap;
	private Map eqRReasonMap;
	private String exportFlag;
	private String outmatchModelId;
	public String outputIoName;
	private String searchOrExport;
}
